import Vue from 'vue'
import Router from 'vue-router'

import Index from '@/components/Index'
import LoginAuth from '@/components/LoginAuth'
import User from '@/components/User'
import Register from '@/components/Register'
import CommonVar from '@/components/CommonVar'
import NavBar from '@/components/NavBar'
import PostingManage from '@/components/PostingManage'
import PostingEdit from '@/components/PostingEdit'
import MyReply from '@/components/MyReply'
import BoardManage from '@/components/BoardManage'
import Posting from '@/components/Posting'
import Board from '@/components/Board'
import Page404 from '@/components/Page404'
import Logout from '@/components/Logout'
import Background from '@/components/Background'
import OtherReply from '@/components/OtherReply'
import Favo from '@/components/Favo'
Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/LoginAuth'
    },
    {
      path: '/Index',
      components: {
        nav: NavBar,
        default: Index
      },
      meta: { title: '科技论坛主页' }
    },
    {
      path: '/LoginAuth',
      components: { default: LoginAuth, nav: NavBar, background: Background },
      meta: { title: '登录页面' }

    },
    {
      path: '/Register',
      components: { default: Register, nav: NavBar },
      meta: {
        title: '注册页面'
      }
    },
    {
      path: '/Logout',
      component: Logout
    },
    {
      path: '/User',
      components:
      {
        nav: NavBar,
        default: User
      },
      meta: {
        title: '用户信息页面',
        requireAuth: true
      }
    },
    {
      path: '/Board/:boardId',
      components: {
        nav: NavBar,
        default: Board
      }
    },
    {
      path: '/BoardManage',
      components: {
        nav: NavBar,
        default: BoardManage
      },
      meta: {
        title: '版块管理页面',
        requireAuth: true
      }
    },
    {
      path: '/PostingManage',
      components: {
        nav: NavBar,
        default: PostingManage
      },
      meta: {
        title: '帖子管理页面',
        requireAuth: true
      }
    },
    {
      path: '/PostingEdit',
      components: {
        nav: NavBar,
        default: PostingEdit
      },
      meta: {
        title: '帖子编辑页面',
        requireAuth: true
      }
    },
    {
      path: '/MyReply',
      components: {
        nav: NavBar,
        default: MyReply
      },
      meta: {
        title: '历史回复',
        requireAuth: true
      }
    },
    {
      path: '/OtherReply',
      components: {
        nav: NavBar,
        default: OtherReply
      },
      meta: {
        title: '他人回复',
        requireAuth: true
      }
    },
    { path: '/404', component: Page404, name: 'Page404' },
    {
      path: '/Posting/:postingId',
      components: {
        nav: NavBar,
        default: Posting
      }
    },
    {
      path: '/Favo',
      components: {
        nav: NavBar,
        default: Favo
      }
    }
  ]
})

router.beforeEach((to, from, next) => {
  console.log(from)
  if (to.matched.length === 0) { // 判断此跳转路由的来源路由是否存在，存在的情况跳转到来源路由，否则跳转到404页面
    next('/404')
  }
  if (to.matched.some(res => res.meta.requireAuth)) { // 判断地址是否需要登录权限
    if (CommonVar.UserInformation.userId !== null) { // 判断是否登录
      next()
    } else { // 没登录则跳转到登录界面
      alert('需要登录!')
      console.log(CommonVar)
      CommonVar.lastUrl = to.fullPath // 保存登陆前的页面
      next({
        path: '/LoginAuth'
      })
    }
  } else {
    next()
  }
})

export default router
